﻿#include "OiPCH.hpp"

namespace Oi
{
    namespace PLScannerCore
    {
        class PresetParamsManagerImpl : public Impl<PresetParamsManager>
        {
        public:
            QMap<QString, PresetParams> _params;
        };

        PresetParamsManager::PresetParamsManager()
        {
            OI_I(PresetParamsManager);

            loadParams();
        }

        bool PresetParamsManager::isValid()
        {
            OI_F(PresetParamsManager);
            return !d->_params.isEmpty();
        }

        void PresetParamsManager::loadParams()
        {
            OI_F(PresetParamsManager);
            d->_params.clear();

            auto paramsDir = PLScanner::instance()->productDefaultDir;

            if(!QDir(paramsDir).exists())
            {
                QDir().mkpath(paramsDir);
            }

            auto fileName = paramsDir + "/preset.json";

            if(!QFileInfo::exists(fileName))
            {
                return;
            }

            QFile jsonReadFile(fileName);

            QByteArray bytes;

            if(jsonReadFile.open(QFile::ReadOnly))
            {
                bytes = jsonReadFile.readAll();
                jsonReadFile.close();
            }
            else
            {
                return;
            }

            QJsonParseError jsonError;

            QJsonDocument doucment = QJsonDocument::fromJson(bytes, &jsonError);

            if(doucment.isNull() || jsonError.error != QJsonParseError::NoError)
                return;

            if(doucment.isArray())
            {
                auto ms = doucment.array();
                for(auto& m : ms)
                {
                    auto mo = m.toObject();
                    if(mo.contains("Name"))
                    {
                        auto& param = d->_params[mo["Name"].toString()];

                        if (mo.contains("Tools"))
                        {
                            auto mts = mo["Tools"].toArray();
                            for (auto& mt : mts)
                            {
                                auto mto = mt.toObject();

                                if(mto.contains("ToolName"))
                                {
                                    auto& presetTool = param[mto["ToolName"].toString()];

                                    if(mto.contains("ToolType")) presetTool.toolType = mto["ToolType"].toInt();
                                    if(mto.contains("Config"))
                                    {
                                        presetTool.config = mto["Config"].toObject().toVariantMap();

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        void PresetParamsManager::saveParams()
        {
            OI_F(PresetParamsManager);

            QJsonArray ms;

            for(auto& opi = d->_params.begin(); opi != d->_params.end(); opi++)
            {
                QJsonObject mo;
                mo["Name"] = opi.key();

                QJsonArray mta;
                for (auto& oti = opi->begin(); oti != opi->end(); oti++)
                {
                    QJsonObject mto;
                    mto["ToolName"] = oti.key();
                    mto["ToolType"] = oti->toolType;

                    QJsonObject mca = QJsonObject::fromVariantMap(oti->config);

                    mto["Config"] = mca;
                    mta.append(mto);
                }
                mo["Tools"] = mta;
                ms.append(mo);
            }

            QJsonDocument document;
            document.setArray(ms);
            QByteArray jsonByteArray = document.toJson(QJsonDocument::Indented);

            auto paramsDir = PLScanner::instance()->productDefaultDir;

            auto fileName = paramsDir + "/preset.json";

            QFile jsonWriteFile(fileName);

            if(!jsonWriteFile.open(QFile::WriteOnly))
            {
                return;
            }

            jsonWriteFile.write(jsonByteArray);

            jsonWriteFile.close();
        }

        bool PresetParamsManager::addParams(const QString& name, const PresetParams & params)
        {
            OI_F(PresetParamsManager);

            if (d->_params.contains(name))
            {
                return false;
            }
           
            d->_params[name] = params;

            return true;
        }

        bool PresetParamsManager::delParams(const QString& name)
        {
            OI_F(PresetParamsManager);

            if(!d->_params.contains(name))
            {
                return false;
            }
            d->_params.remove(name);

            return true;
        }

        bool PresetParamsManager::setParams(const QString& name, const PresetParams & params)
        {
            OI_F(PresetParamsManager);

            if(!d->_params.contains(name))
            {
                return false;
            }
            d->_params[name] = params;

            return true;
        }

        QStringList PresetParamsManager::presetNames() const
        {
            return OI_IMPL()->_params.keys();
        }

        PresetParams& PresetParamsManager::params(QString name)
        {
            OI_F(PresetParamsManager);
            if(d->_params.contains(name))
            {
                return d->_params[name];
            }
            else
            {
                return PresetParams();
            }
        }

        QMap<QString, PresetParams>& PresetParamsManager::paramsMap()
        {
            return OI_IMPL()->_params;
        }

        PresetParamsManager* PresetParamsManager::instance()
        {
            static PresetParamsManager s_preset;
            return &s_preset;
        }

        

    }
}